/**
 * 
 */
package org.bankabeule.ui.views;

import org.bankabeule.Model;
import org.bankabeule.core.ModelListener;
import org.bankabeule.core.ModelManager;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

/**
 * @author sdudoit
 *
 */
public abstract class AbstractViewPart extends ViewPart implements ModelListener {

	
	/**
	 * Constructor
	 */
	public AbstractViewPart() {
		// register listener
		ModelManager.get().addModelListener(this);
	}
	

	@Override
	public void dispose() {
		// unregister listener
		ModelManager.get().removeModelListener(this);
		// dispose
		super.dispose();
	}	
	
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
	}

	/* (non-Javadoc)
	 * @see org.bankabeule.core.ModelListener#modelLoaded(org.bankabeule.Model)
	 */
	public void modelLoaded(Model model) {
		String name = getPartName();
		if (name.indexOf(" * ") > -1) {
			setPartName(name.substring(0, name.indexOf(" * ")));
		}
	}


	/* (non-Javadoc)
	 * @see org.bankabeule.core.ModelListener#modelUnloaded(org.bankabeule.Model)
	 */
	public void modelUnloaded(Model model) {
		String name = getPartName();
		if (name.indexOf(" * ") > -1) {
			setPartName(name.substring(0, name.indexOf(" * ")));
		}
	}

	
	public void modelDirty(Model model) {
		setPartName(getPartName()+" * ");
	}
	
	
	public void modelSaved(Model model) {
		String name = getPartName();
		if (name.indexOf(" * ") > -1) {
			setPartName(name.substring(0, name.indexOf(" * ")));
		}
	}

}
